from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import os
from drf_movie_rimi.settings import BASE_DIR
from .models import FileTestImage
"""
django 最原始的文件处理方式
"""


@csrf_exempt
def test_files(request):

    data = request.FILES['test_files']
    # write_files_out_side(request.FILES['test_files'])
    FileTestImage.objects.create(name='测试1',img=data)


    return HttpResponse('hello world')


def write_files(file):
    media_dir = os.path.join(BASE_DIR, 'media', 'test1')
    media_file = os.path.join(media_dir, file.name)

    with open(media_file, 'wb+') as f:
        #断点查看i
        for i in file.chunks():
            f.write(i)


def write_files_out_side(file):
    media_dir = os.path.dirname(BASE_DIR)
    media_file = os.path.join(media_dir, 'test_store',file.name)

    with open(media_file, 'wb+') as f:
        #断点查看i
        for i in file.chunks():
            f.write(i)



